package com.example.safeassistan.home.adapter;


import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.safeassistan.R;
import com.example.safeassistan.home.model.DeviceListModel;

import java.util.List;

public class DeviceFragmentAdapter extends BaseRecyclerViewAdapter<DeviceListModel.ResultDTO.RecordsDTO> {
    private List<DeviceListModel.ResultDTO.RecordsDTO> list;
    private Context context;
    private OnDeleteClickLister mDeleteClickListener;

    public DeviceFragmentAdapter(List<DeviceListModel.ResultDTO.RecordsDTO> data, Context context) {
        super(context, data, R.layout.item_device_fragment);
        this.context=context;
        this.list=data;
    }


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    protected void convert(BaseViewHolder helper, DeviceListModel.ResultDTO.RecordsDTO item) {

        helper.addOnClickListener(R.id.tvRepair);
        helper.addOnClickListener(R.id.tvUnBind);
    }

    @Override
    protected void onBindData(RecyclerViewHolder holder, DeviceListModel.ResultDTO.RecordsDTO bean, int position) {
        TextView tvRepair = (TextView) holder.getView(R.id.tvRepair);
        TextView tvUnBind= (TextView) holder.getView(R.id.tvUnBind);
        TextView tvDeviceName=(TextView)holder.getView(R.id.tvDeviceName);
        ImageView ivDeviceImg=(ImageView) holder.getView(R.id.ivDeviceImg);
        TextView tvVersion=(TextView)holder.getView(R.id.tvVersion);
        TextView tvDeviceId=(TextView)holder.getView(R.id.tvDeviceId);
        TextView tvDeviceBrand=(TextView)holder.getView(R.id.tvDeviceBrand);


        Glide.with(context).load(bean.getDeviceImg()).placeholder(R.mipmap.default_image)
                .into(ivDeviceImg);
        tvDeviceName.setText(bean.getName());
        tvVersion.setText("型号："+bean.getVersion());
        tvDeviceId.setText("编号："+bean.getCode());
        tvDeviceBrand.setText("品牌："+bean.getBrand());


//        tvRepair.setTag(position);
//        if (!tvRepair.hasOnClickListeners()) {
//            tvRepair.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (mDeleteClickListener != null) {
//                        mDeleteClickListener.onDeleteClick(v, (Integer) v.getTag());
//                    }
//                }
//            });
//        }
        tvUnBind.setTag(position);
        if (!tvUnBind.hasOnClickListeners()) {
            tvUnBind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDeleteClickListener != null) {
                        mDeleteClickListener.onDeleteClick(v, (Integer) v.getTag());
                    }
                }
            });
        }
    }

    public void setOnDeleteClickListener(OnDeleteClickLister listener) {
        this.mDeleteClickListener = listener;
    }

    public interface OnDeleteClickLister {
        void onDeleteClick(View view, int position);
    }
}
